package com.ruoyi.system.service.impl.ingredientsServiceImpl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.ingredients.PartIngredients;
import com.ruoyi.system.mapper.ingredients.PartIngredientsDetailMapper;
import com.ruoyi.system.mapper.ingredients.PartIngredientsMapper;
import com.ruoyi.system.service.ingredientsService.IPartIngredientsService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.Validate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 辅料Service业务层处理
 * 
 * @author ljx
 * @date 2020-07-01
 */
@Service
public class PartIngredientsServiceImpl implements IPartIngredientsService
{
    @Resource
    private PartIngredientsMapper partIngredientsMapper;
    @Resource
    private PartIngredientsDetailMapper partIngredientsDetailMapper;

    /**
     * 查询辅料
     * 
     * @param id 辅料ID
     * @return 辅料
     */
    @Override
    public PartIngredients selectPartIngredientsById(Long id)
    {
        return partIngredientsMapper.selectPartIngredientsById(id);
    }

    /**
     * 查询辅料列表
     * 
     * @param partIngredients 辅料
     * @return 辅料
     */
    @Override
    public List<PartIngredients> selectPartIngredientsList(PartIngredients partIngredients)
    {
        return partIngredientsMapper.selectPartIngredientsList(partIngredients);
    }
    
    @Override
    public List<PartIngredients> selectPartIngredientsListAllByName(PartIngredients partIngredients)
    {
    	return partIngredientsMapper.selectPartIngredientsListAllByName(partIngredients);
    }
    
    @Override
    public List<PartIngredients> selectPartIngredientsListAll() {
    	return partIngredientsMapper.selectPartIngredientsListAll();
    }

    /**
     * 新增辅料
     * 
     * @param partIngredients 辅料
     * @return 结果
     */
    @Override
    public AjaxResult insertPartIngredients(PartIngredients partIngredients)
    {
        PartIngredients ingredients = new PartIngredients();
        ingredients.setName(partIngredients.getName());
        ingredients.setSubName(partIngredients.getSubName());
        List<PartIngredients> list = partIngredientsMapper.selectPartIngredientsListBySubNameAndName(ingredients);
        if(CollectionUtils.isNotEmpty(list)){
            return AjaxResult.error("辅料子名称重复");
        }
        partIngredients.setCreateTime(DateUtils.getNowDate());
        partIngredientsMapper.insertPartIngredients(partIngredients);
        return AjaxResult.success();
    }

    /**
     * 修改辅料
     * 
     * @param partIngredients 辅料
     * @return 结果
     */
    @Override
    public AjaxResult updatePartIngredients(PartIngredients partIngredients)
    {
    	
    	  PartIngredients ingredients = new PartIngredients();
          ingredients.setName(partIngredients.getName());
          ingredients.setSubName(partIngredients.getSubName());
          List<PartIngredients> list = partIngredientsMapper.selectPartIngredientsListBySubNameAndName(ingredients);
          if(CollectionUtils.isNotEmpty(list)){
        	  PartIngredients partIngredients2 = list.get(0);
        	  if (partIngredients2.getId().longValue() == partIngredients.getId().longValue()) {
				
        	  }else{
        		  return AjaxResult.error("辅料子名称重复");
        	  }
              
          }
          
        partIngredients.setUpdateTime(DateUtils.getNowDate());
        partIngredientsMapper.updatePartIngredients(partIngredients);
        return AjaxResult.success();
    }

    /**
     * 删除辅料对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePartIngredientsByIds(String ids)
    {
    	int selectPartIngredientsDetailByPartIds = partIngredientsDetailMapper.selectPartIngredientsDetailByPartIds(Convert.toStrArray(ids));
    	if (selectPartIngredientsDetailByPartIds > 0) {
			Validate.isTrue(false ,"已经有库存记录不能删除");
		}
        return partIngredientsMapper.deletePartIngredientsByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除辅料信息
     * 
     * @param id 辅料ID
     * @return 结果
     */
    @Override
    public int deletePartIngredientsById(Long id)
    {
        return partIngredientsMapper.deletePartIngredientsById(id);
    }
}
